package ua.yandex.shad.tempseries;

import static java.lang.Math.abs;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TemperatureSeriesAnalysis {

    private final double[] temperatureSeries;
    final int minTemperature = -273;

    public TemperatureSeriesAnalysis(double[] temperatureSeries) {
        boolean flag = true;
        for (int index = 0; index < temperatureSeries.length && flag == true; index++) {
            if (temperatureSeries[index] < minTemperature) {
                flag = false;
            }
        }
        if (flag == false) {
            try {
                throw new MyException("IllegalTemperatureValueException");
            } catch (MyException ex) {
                Logger.getLogger(TemperatureSeriesAnalysis.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        this.temperatureSeries = temperatureSeries;
    }

    public double calcAverageTemp() {
        double sum;
        sum = 0;
        if (temperatureSeries.length == 0) {
            return sum;
        }
        for (double d : temperatureSeries) {
            sum += d;
        }
        return sum / temperatureSeries.length;
    }

    public double calcTempDeviation() {
        if (temperatureSeries.length == 0) {
            return 0;
        }
        double av = this.calcAverageTemp();
        double sum = 0;
        for (double d : temperatureSeries) {
            sum += (d - av) * (d - av);
        }
        return sum / temperatureSeries.length;
    }

    public double findTempClosestToValue(double tempValue) {
        if (temperatureSeries.length == 0) {
            return 0;
        }
        double minAbsTemp = temperatureSeries[0];
        for (double d : temperatureSeries) {
             if( abs(minAbsTemp-tempValue) > abs(d-tempValue))  minAbsTemp = d;
             if ((abs(minAbsTemp - tempValue) == abs(d - tempValue)) && ((d - tempValue) * (minAbsTemp - tempValue) < 0)) {
                minAbsTemp = abs(minAbsTemp);
            }
        }
        return minAbsTemp;
    }

    public double findTempClosestToZero() {
        double minAbsTemp = findTempClosestToValue(0);
        return minAbsTemp;
    }

    double[] findTempsLessThen(double tempValue) {
        int leng;
        leng = temperatureSeries.length;
        double[] lessThen;
        if (leng == 0) {
            lessThen = new double[1];
            lessThen[0] = 0;
            return lessThen;
        }
        int counter = 0;
        for (double d : temperatureSeries) {
            if (d <= tempValue) {
                counter++;
            }
        }
        lessThen = new double[counter];
        counter=0;
        for (double d : temperatureSeries) {
            if (d <= tempValue) {
                lessThen[counter] = d;
                counter++;
            }
        }
        return lessThen;
    }

    double[] findTempsGreaterThen(double tempValue) {
        int leng;
        leng = temperatureSeries.length;
        double[] greaterThen;
        if (leng == 0) {
            greaterThen = new double[1];
            greaterThen[0] = 0;
            return greaterThen;
        }
        int counter = 0;
        for (double d : temperatureSeries) {
            if (d >= tempValue) {
               counter++;
            }
        }
        greaterThen = new double[counter];
        counter=0;
        for (double d : temperatureSeries) {
            if (d >= tempValue) {
                greaterThen[counter] = d;
                counter++;
            }
        }
        return greaterThen;
    }

}
